package src.unitTest;

import org.junit.Test;

import java.util.*;

public class usage {

    /**
     * 测试将dict转化为list
     * dict.keySet() dict.values()两个方法分别得到key与value List
     */
    @Test
    public void test_dict2list(){
        Map<String, Double> dict = new HashMap<>();
        dict.put("name", 10d);
        dict.put("age", 20d);
        dict.put("hello", 230d);

        List<String> mapKeyList = new ArrayList(dict.keySet());
        System.out.println("mapKeyList " + mapKeyList);
        List<Double> mapValueList = new ArrayList(dict.values());
        System.out.println("mapValuesList " + mapValueList);
    }

    /**
     * 如何将tuple 按照指定的顺序排列
     *
     */
    @Test
    public void test_sort_tuple(){
        List<String> keys = Arrays.asList("name", "age", "hello");
        List<Double> values = Arrays.asList(20.0002d, 20d, 20.0001d);
        int length = keys.size();
        List<TermTuple> termTuples = new LinkedList<>();
        for(int i = 0; i < length; i++){
            termTuples.add(new TermTuple(keys.get(i), values.get(i)));
        }
        // 对于termTuples进行排序
        Collections.sort(termTuples);
        for (TermTuple item : termTuples){
            System.out.println(item);
        }
    }

    /**
     * 用来构建基础的元组
     */
    public class TermTuple implements Comparable<TermTuple>{
        private String key;
        private double value;

        public TermTuple(String key, double value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public double getValue() {
            return value;
        }

        @Override
        public int compareTo(TermTuple termTuple) {
            return -1 * new Double(this.value).compareTo(termTuple.value);
        }

        @Override
        public String toString() {
            return "TermTuple{" +
                    "key='" + key + '\'' +
                    ", value=" + value +
                    '}';
        }
    }
}
